stages:
  - push_chart
  - deploy

variables:
  VERSION: 1.0.${CI_PIPELINE_ID}

zip_chart:
  stage: push_chart
  before_script:
    - curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
    - chmod 700 get_helm.sh
    - ./get_helm.sh 
  script:
    - helm package infra/momo-store-chart
    - mv momo-store-0.1.0.tgz momo-store-${VERSION}.tgz
    - curl -u ${NEXUS_REPO_USER}:${NEXUS_REPO_PASS} http://nexus.k8s.praktikum-services.tech/repository/momo-tsarev/ --upload-file momo-store-${VERSION}.tgz
deploy:
  stage: deploy
  image: alpine/k8s:1.22.15
  environment:
    name: k8s
    url: https://std-015-37.k8s.praktikum-services.tech
  when: manual
  before_script:
    - mkdir -p ~/.kube
    - chmod 700 ~/.kube
    - echo "$KUBECONFIG" >> ~/.kube/config
    - echo "$KUBECERT" >> ~/.kube/ca.pem
    - export KUBECONFIG=~/.kube/config
    - chmod 644 ~/.kube/config ~/.kube/ca.pem
  script:
    - kubectl config get-contexts
    - helm repo add nexus http://${NEXUS_REPO_USER}:${NEXUS_REPO_PASS}@nexus.k8s.praktikum-services.tech/repository/momo-tsarev/
    - helm repo update
    - helm list
    - curl -u ${NEXUS_REPO_USER}:${NEXUS_REPO_PASS} https://nexus.k8s.praktikum-services.tech/repository/momo-tsarev/momo-store-${VERSION}.tgz -o momo-store-${VERSION}.tgz
    # - helm uninstall momo-store
    - helm upgrade --install momo-store momo-store-${VERSION}.tgz -n std-015-37
